package com.heima.kafka.sample;

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.TimeWindows;
import org.apache.kafka.streams.kstream.ValueMapper;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

/**
 * @author VectorX
 * @version V1.0
 * @description
 * @date 2024-05-22 15:19:30
 */
public class KafkaStreamQuickStart
{
    public static void main(String[] args) {
        // kafka的配置核心
        final Properties prop = new Properties();
        prop.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.56.17:9092");
        prop.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes
                .String()
                .getClass());
        prop.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes
                .String()
                .getClass());
        prop.put(StreamsConfig.APPLICATION_ID_CONFIG, "stream-quickstart");

        // stream构建器
        final StreamsBuilder streamsBuilder = new StreamsBuilder();

        // 流式计算
        streamProcessor(streamsBuilder);

        // 创建kafkaStream对象
        final KafkaStreams kafkaStreams = new KafkaStreams(streamsBuilder.build(), prop);
        // 开启流式计算
        kafkaStreams.start();
    }

    /**
     * 流式计算
     *
     * @param streamsBuilder 流生成器
     */
    private static void streamProcessor(StreamsBuilder streamsBuilder) {
        // 创建kstream对象，同时指定从那个topic中接收消息
        final KStream<String, String> stream = streamsBuilder.stream("itcast-topic-input");

        // 处理消息的value
        stream.flatMapValues((ValueMapper<String, Iterable<String>>) value -> Arrays.asList(value.split(" ")))
              // 按照value进行聚合处理
              .groupBy((key, value) -> value)
              // 时间窗口
              .windowedBy(TimeWindows.of(Duration.ofSeconds(10)))
              // 统计单词的个数
              .count()
              // 转换为kStream
              .toStream()
              .map((key, value) -> {
                  System.out.println("key:" + key + ",vlaue:" + value);
                  return new KeyValue<>(key.key(), value.toString());
              })
              // 发送消息
              .to("itcast-topic-out");
    }
}
